package com.mszl.blog.controller;

import com.mszl.blog.entity.Article;
import com.mszl.blog.service.ArticleService;
import com.mszl.blog.vo.Result;
import com.mszl.blog.vo.params.ArticleParam;
import com.mszl.blog.vo.params.PageParams;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("articles")
public class ArticleController {


    @Resource
    private ArticleService articleService;




    //    首页文章列表
    @PostMapping("list")
    public Result listAtricle(@RequestBody PageParams pageParams){

        return articleService.listArticle(pageParams);



    }
//    首页最热文章

    @PostMapping("hot")
    public Result hotAtricle(){

        int limit=5;
        return articleService.hotAtricle(limit);



    }

//    最新文章
    @PostMapping("new")
    public Result newAtricle(){

        int limit=5;
        return articleService.newAtricle(limit);



    }
//    通过id查看文章
   @GetMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long id){
        return articleService.findArticleById(id);
    }

//    @GetMapping
//    public ResponseEntity<Page<Article>> queryByPage(Article article, PageRequest pageRequest) {
//        return ResponseEntity.ok(this.articleService.queryByPage(article, pageRequest));
//    }






    @GetMapping("{id}")
    public ResponseEntity<Article> queryById(@PathVariable("id") Long articleId) {
        return ResponseEntity.ok(this.articleService.queryById(articleId));
    }

    @PostMapping("add")
    public ResponseEntity<Article> add(@RequestBody Article article) {
        return ResponseEntity.ok(this.articleService.insert(article));
    }


    @PostMapping("search")
    public Result serch(@RequestBody String key){
        return articleService.findArticleByKey(key);
    }




}
